/*
 * Copyright (c) 2025, 廖树财, <study_2030@163.com>
 *
 * PES1.0.3-YCYW is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2     
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#ifndef _BASE_INFO_FUN
#define _BASE_INFO_FUN
#include <time.h>
#include "define.h"

// 存储CPU时间信息的结构体
typedef struct
{
    unsigned long utime;
    unsigned long stime;
    struct timespec timestamp;
} cpu_time_t;

// 网卡单元定义
typedef struct _S_NETCARD
{
    char iface[255];
    char ip[65];
    char mac[32];
    char netmask[65];
    char gateway[65];
    char subnetMask[65];
} S_NETCARD, *PS_NETCARD;
typedef struct _S_4GModule
{
    char iface[255];
    int bandWidth;
    char imsi[128];
    char imei[32];
    char ip[65];
    char manufacture[128];
    char model[128];
    char netType[128];
    char simId[128];
    char carrierInformation[32];
} S_4GModule, *PS_4GModule;
// 终端校时

typedef struct _S_TERMINAL_CHECKTIME_REQ
{
    char mid[128];
    char service[32];
    char cmd[32];
    char ntpAddress[32];
    int ntpPort;
    char zone[32];
    char node[32];
    char deviceSn[32];
    char deviceId[32];
    char requestId[32];
    char msgTime[32];
} S_TERMINAL_CHECKTIME_REQ, *PS_TERMINAL_CHECKTIME_REQ;
// 终端管理业务基础信息
typedef struct _S_TERMINAL_MANAGE
{
    int iCPUCores;
    int iCPUCount;
    int iNetCardCount;
    char cCPUArch[32];
    char cCPUFrequency[32];
    char cCPUModel[32];
    char cdiskCapacity[32];
    char cfirmwareVersion[128];
    char cmemCapacity[32];
    char cosKernelVer[64];
    char cosReleaseName[64];
    char cosVersion[64];
    char cpatchVersion[64];
    S_NETCARD sNetCard[100];
    S_4GModule s4GModule[3];
} S_TERMINAL_MANAGE, *PS_TERMINAL_MANAGE;
// 运行数据事件单元定义
typedef struct _S_DATA_REPORT_CMD
{
    int sampleFrequency;
    int reportFrequency;
    char indicator[64];
} S_DATA_REPORT_CMD, *PS_DATA_REPORT_CMD;
// 运行数据参数单元定义
typedef struct _S_DATA_REPORT_PARAM
{
    double min;
    double max;
    double current;
    long long sum;
    int count;
} S_DATA_REPORT_PARAM, *PS_DATA_REPORT_PARAM;
// 一般运行数据上报定义
typedef struct _S_DATA_REPORT_NORMAL
{
    double min;
    double max;
    double current;
    double avg;
    char indicator[256];
    char unit[16];
} S_DATA_REPORT_NORMAL, *PS_DATA_REPORT_NORMAL;
// mem运行数据上报定义
typedef struct _S_DATA_REPORT_MEM_SUM
{
    int total;
    int free;
    int used;
    int available;
    int shared;
    int buff_cache;
} S_DATA_REPORT_MEM_SUM, *PS_DATA_REPORT_MEM_SUM;
// top5
typedef struct _S_DATA_TOP5
{
    char pid[16];
    char pName[32];
    char user[16];
    char usage[16];

} S_DATA_TOP5, *PS_DATA_TOP5;
typedef struct _S_DATA_REPORT_TOP5
{
    S_DATA_REPORT_MEM_SUM mem;
    S_DATA_REPORT_NORMAL normal;
    S_DATA_TOP5 top[10];
} S_DATA_REPORT_TOP5, *PS_DATA_REPORT_TOP5;
// 4G运行数据上报定义
typedef struct _S_DATA_REPORT_4G
{
    long rxBytes;
    long txBytes;
    long rxPackets;
    long txPackets;
    char iface[64];
    char rsrp[16];
    char rsrq[16];
    char rssi[16];
    char sinr[16];
    char upSpeed[16];
    char downSpeed[16];
    char speed[16];
} S_DATA_REPORT_4G, *PS_DATA_REPORT_4G;
// ETH运行数据上报定义
typedef struct _S_DATA_REPORT_WIRE
{
    time_t second;
    long rxBytes;
    long txBytes;
    long rxPackets;
    long txPackets;
    long upSpeed;
    long downSpeed;
    char iface[64];

} S_DATA_REPORT_WIRE, *PS_DATA_REPORT_WIRE;

typedef struct _S_DATA_REPORT_TRAFFIC
{
    time_t second;
    unsigned long long rxBytes;
    unsigned long long txBytes;
    unsigned long long deltatx;
    unsigned long long deltarx;
    unsigned long long total;
    char iface[64];

} S_DATA_REPORT_TRAFFIC, *PS_DATA_REPORT_TRAFFIC;
// 磁盘运行数据上报定义
typedef struct _S_DATA_REPORT_DISK
{
    char available[16];
    char availablePercent[16];
    char fileSystem[32];
    char mountedOn[255];
    char size[16];
    char used[16];
    char usedPercent[16];
} S_DATA_REPORT_DISK, *PS_DATA_REPORT_DISK;

// 磁盘运行数据上报定义
typedef struct _S_DATA_REPORT_DISK_ALARM
{
    char mountedOn[255];
    int usedPercent;
} S_DATA_REPORT_DISK_ALARM, *PS_DATA_REPORT_DISK_ALARM;

// 磁盘运行数据上报定义
typedef struct _S_DATA_REPORT_DISK2
{
    int count;
    S_DATA_REPORT_DISK disk[200];
} S_DATA_REPORT_DISK2, *PS_DATA_REPORT_DISK2;
// 磁盘运行数据上报定义
typedef struct _S_DATA_REPORT_DISK_SUM
{
    char total[16];
    char free[16];
    char used[16];
    char available[16];
} S_DATA_REPORT_DISK_SUM, *PS_DATA_REPORT_DISK_SUM;
// 网络运行数据上报定义
typedef struct _S_DATA_REPORT_NETWORK
{
    int ttl;
    double lossPct;
    int sent;
    int recv;
    double avg;
    double best;
    double worst;
    double stddev;
    char host[64];
    char target[64];
} S_DATA_REPORT_NETWORK, *PS_DATA_REPORT_NETWORK;
// 进程数据上报定义
typedef struct _S_DATA_REPORT_PROCESS
{
    char pid[16];
    char pName[32];
    char state[32];
    char started[32];
    char uid[16];
    char user[32];
    char gid[16];
    char group[32];
    char ppid[16];
    char cpuUsage[16];
    char memUsage[16];
    char cmd[256];
    char procMd5[128];
} S_DATA_REPORT_PROCESS, *PS_DATA_REPORT_PROCESS;
// 进程数据上报定义2
typedef struct _S_DATA_REPORT_PROCESS2
{
    char pid[16];
    char cpuUsage[16];
    char memUsage[16];
    char diskUsage[16];
    char delayTime[16];
    char lossUsage[16];
    char user[16];
    char status[16];
} S_DATA_REPORT_PROCESS2, *PS_DATA_REPORT_PROCESS2;
// 进程数据上报定义2
// 服务数据上报定义
typedef struct _S_DATA_REPORT_SERVER
{
    char pid[16];
    char status[16];
    char isAutoStarted[16];
    char procotolName[16];
    char protocolType[16];
    char protocolVersion[16];
    char installedTime[32];
    char updatedTime[32];
    char description[128];
    char name[128];
    char path[255];
} S_DATA_REPORT_SERVER, *PS_DATA_REPORT_SERVER;
// 端口数据上报定义
typedef struct _S_DATA_REPORT_PORT
{
    char localIp[64];
    char localPort[16];
    char foreignIp[64];
    char foreignPort[16];
    char protocol[16];
    char state[16];
    int pid;
    char pName[64];
} S_DATA_REPORT_PORT, *PS_DATA_REPORT_PORT;
// 心跳数据上报定义
typedef struct _S_DATA_REPORT_ALIVE
{

} S_DATA_REPORT_ALIVE, *PS_DATA_REPORT_ALIVE;
// 进程数据上报定义
typedef struct _S_DATA_REPORT_PROCESS_EVENT
{
    int isFull;
    char eventType[32];
    S_DATA_REPORT_PROCESS process;
} S_DATA_REPORT_PROCESS_EVENT, *PS_DATA_REPORT_PROCESS_EVENT;
// 服务数据上报定义
typedef struct _S_DATA_REPORT_SERVER_EVENT
{
    int isFull;
    char eventType[32];
    S_DATA_REPORT_SERVER server;
} S_DATA_REPORT_SERVER_EVENT, *PS_DATA_REPORT_SERVER_EVENT;
// 端口数据上报定义
typedef struct _S_DATA_REPORT_PORT_EVENT
{
    char eventType[32];
    int isFull;
    S_DATA_REPORT_PORT port;
} S_DATA_REPORT_PORT_EVENT, *PS_DATA_REPORT_PORT_EVENT;
// container上报定义
typedef struct _S_DATA_REPORT_CONTAINER_EVENT
{
    char id[64];          // 只在端侧docket使用id
    char containerId[70]; // 平台侧下发的唯一标识
    char name[32];
    char version[32];
    char type[32];
    char manufacture[64];
    char status[16];
    char deployTime[32];
    double cpuUsed;
    double memUsed;
    int diskUsed;
    char image[2][32];
} S_DATA_REPORT_CONTAINER_EVENT, *PS_DATA_REPORT_CONTAINER_EVENT;
// app上报定义
typedef struct _S_DATA_REPORT_APP_EVENT
{
    int pid;
    ///
    char appId[64];
    char name[128];
    char version[32];
    char type[32];
    char manufacture[64];
    char status[16];
    char deployTime[32];
    double cpuUsed;
    double memUsed;
    double diskUsed;
    int isTrustworthy;
} S_DATA_REPORT_APP_EVENT, *PS_DATA_REPORT_APP_EVENT;
// app上报定义
typedef struct _S_DATA_REPORT_APP_EVENT2
{
    char pid[16];
    S_DATA_REPORT_APP_EVENT data;
} S_DATA_REPORT_APP_EVENT2, *PS_DATA_REPORT_APP_EVENT2;

typedef struct ST_ACQUISITION
{
    int gwreginfo;
    int iotfile;
    int romacert;
    int eagleeyessl;
} ST_ACQUISITION, *PST_ACQUISITION;

int testFun(int test);

int setNetIp(char *ip);

int syncTime(char *timec, char *result);

int systemDate4(char *result);

int popenCmd(char *cmd, char *result);
// 如果本机的IP地址绑定在第一块网卡上，则只需指定网卡名称，无需获取所有网卡的信息即可获取
int get_localip(const char *eth_name, char *local_ip_addr);

// 获取网卡对应IP地址[其中eth是网卡名字 如“eth0” “eth1” "en0" "en1"等, ipstr是输出参数]
// 获取指定网络接口的ip地址;
int get_local_ip(const char *eth_inf, char *ipstr);
// 获取指定网络接口的mac地址;
int get_local_mac(const char *eth_inf, char *mac);

int cpuModel(char *result);

int cpuArch(char *result);

int cpuCount(char *result);

int cpuCores(char *result);

int cpuFrequency(char *result);

int cpuTemp(char *result);

int cpuUsed(char *result);

int memUsed(char *result);

int memCapacity(char *result);

int diskUsed(char *result);

int diskCapacity(char *result);

int osKernelVer(char *result);

int osReleaseName(char *result);

int osVersion(char *result);

int osVersion_V1(char *result);

int wiredModules(PS_NETCARD result);

int _4gModules(PS_4GModule result);

int _4GInfoReport(PS_DATA_REPORT_4G result);

int reBoot(int relayTime, char *result);

int syncNtp(char *ip, char *result);

int systemDate(char *result);

int systemDate2(char *result);

int systemDate3(char *result);

int rxBytes(char *eth, char *result);

int txBytes(char *eth, char *result);

int rxPackets(char *eth, char *result);

int txPackets(char *eth, char *result);

int diskInfoReport(PS_DATA_REPORT_DISK result);

int diskInfoReport2(PS_DATA_REPORT_DISK result, int count, PS_DATA_REPORT_DISK_SUM result2);

int containerInfoReport(PS_DATA_REPORT_CONTAINER_EVENT result);

int appInfoReport(PS_DATA_REPORT_CONTAINER_EVENT result);

void setDiskReportTime();

int fullInfoReport(char *cmd, void *result);

int dataReport(PS_DATA_REPORT_CMD dataReportCmd, void *dataReport, int now); // return >=1,has >=one stru ;return 0, has no stru;return -1,fail

int updateCmd(PS_DATA_REPORT_CMD dataReportCmd);

int toSetnvrConfig(char *body, char *path, char *resMsg);

int toSetRomaConfig(char *connectserver, char *connectreginfo, char *resMsg);

int toSetEmqxIp(const char *ip);

int toSetEmqxPort(const int port);

int setAuthorized(char *type, char *result, char *portNumber);

int popenSetAuthorized(char *cmd, char *result, char *path);

int serverInfoReport(PS_DATA_REPORT_SERVER_EVENT result);

void monitor_file(const char* filename, int check_interval);

#endif

